Imports DAL
Imports MyClasses
Imports System.Configuration

Public Class frmSetFinancialYear

    Dim dtbFinyrid As DataTable

    Private Sub frmSetFinancialYear_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        MDIPharmacy.boolFinYr = True
    End Sub

    Private Sub frmSetFinancialYear_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Financial Year Ending")
        e.Cancel = True
    End Sub

    Private Sub frmSetFinancialYear_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Financial Year Ending")
        End If
    End Sub

    Private Sub frmSetFinancialYear_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Call BCommon.SetFormStandards(Me)
        Call SetControls()
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub SetControls()
        Try
            dtbFinyrid = DFinancialYear.SetFinyear(ConfigurationManager.AppSettings("FirmId"))
            If dtbFinyrid.Rows.Count > 0 Then
                'lblValCurrentFinYr.Text = dtbFinyrid.Rows(0).Item("Fin_start_dt").date & " TO " & dtbFinyrid.Rows(0).Item("Fin_end_dt").date
                'lblValNewFinYr.Text = dtbFinyrid.Rows(0).Item("Fin_end_dt").date.adddays(1) & " TO " & dtbFinyrid.Rows(0).Item("Fin_end_dt").date.addyears(1)
                lblValCurrentFinYr.Text = dtbFinyrid.Rows(0).Item("Fin_Year_Label")
                lblValNewFinYr.Text = "FY " & dtbFinyrid.Rows(0).Item("Fin_end_dt").date.adddays(1).Year & "-" & dtbFinyrid.Rows(0).Item("Fin_end_dt").date.addyears(1).Year
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetControls")
        End Try
    End Sub

    Private Sub btnProcess_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnProcess.Click
        Dim dtbGetStock As DataTable = New DataTable
        Dim intResult As Integer = 0
        Dim dtbFinyrid As DataTable
        Try
            dtbFinyrid = DFinancialYear.GetFinyears(ConfigurationManager.AppSettings("FinYrId"))
            With dtbFinyrid
                If .Rows.Count > 0 Then
                    'Check Current Date with Financial Year End date 
                    If Now.Date < .Rows(0).Item("Fin_end_dt").date Then
                        If BCommon.ShowMsgError(10428, "YN") = Windows.Forms.DialogResult.Yes Then
                            Dim frmPrg As New frmProgressBar
                            frmPrg.TopMost = True
                            frmPrg.MaximumValue = 10
                            frmPrg.Show()
                            frmPrg.SetProgress(1)
                            frmPrg.SetProgress(2)
                            frmPrg.SetProgress(5)
                            frmPrg.SetProgress(7)
                            BSetFinYear.Updateyears(dtbFinyrid.Rows(0).Item("Fin_end_dt").date.adddays(1), dtbFinyrid.Rows(0).Item("Fin_end_dt").date.addyears(1))
                            dtbGetStock = BSetFinYear.GetStock()
                            frmPrg.SetProgress(8)
                            frmPrg.SetProgress(10)
                            frmPrg.HideProgressbar()

                            If dtbGetStock.Rows.Count > 0 Then
                                intResult = BSetFinYear.SetNewYrStock(dtbGetStock)
                                If intResult > 0 Then
                                    BCommon.ShowMsgError(10406)
                                    Global.System.Windows.Forms.Application.Exit()
                                Else
                                    BCommon.ShowMsgError(4)
                                End If
                            Else
                                BCommon.ShowMsgError(10406)
                                Global.System.Windows.Forms.Application.Exit()
                            End If
                            btnProcess.Enabled = True
                        End If
                    Else
                        Dim frmPrg As New frmProgressBar
                        frmPrg.TopMost = True
                        frmPrg.MaximumValue = 10
                        frmPrg.Show()
                        frmPrg.SetProgress(1)
                        frmPrg.SetProgress(2)
                        frmPrg.SetProgress(5)
                        frmPrg.SetProgress(7)
                        BSetFinYear.Updateyears(dtbFinyrid.Rows(0).Item("Fin_end_dt").date.adddays(1), dtbFinyrid.Rows(0).Item("Fin_end_dt").date.addyears(1))
                        dtbGetStock = BSetFinYear.GetStock()
                        frmPrg.SetProgress(8)
                        frmPrg.SetProgress(10)
                        frmPrg.HideProgressbar()

                        If dtbGetStock.Rows.Count > 0 Then
                            intResult = BSetFinYear.SetNewYrStock(dtbGetStock)
                            If intResult > 0 Then
                                BCommon.ShowMsgError(10406)
                                Global.System.Windows.Forms.Application.Exit()
                            Else
                                BCommon.ShowMsgError(4)
                            End If
                        Else
                            BCommon.ShowMsgError(10406)
                            Global.System.Windows.Forms.Application.Exit()
                        End If
                        btnProcess.Enabled = True
                    End If
                End If
            End With
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnProcess_Click")
        End Try
    End Sub
End Class